package com.gxa.meiye.admin.platform.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.meiye.admin.platform.entity.TConsumer;
import com.gxa.meiye.admin.platform.entity.TTherapist;
import com.gxa.meiye.admin.platform.entity.TOrder;
import com.gxa.meiye.admin.platform.entity.TShop;
import com.gxa.meiye.admin.platform.pojo.vo.DealSituationVo;
import com.gxa.meiye.admin.platform.pojo.vo.RegisterSituationVo;
import com.gxa.meiye.admin.platform.pojo.vo.UserDistributionVo;
import com.gxa.meiye.admin.platform.pojo.vo.UserRegisterVo;
import com.gxa.meiye.admin.platform.service.TConsumerService;
import com.gxa.meiye.admin.platform.service.TOrderService;
import com.gxa.meiye.admin.platform.service.TShopService;
import com.gxa.meiye.admin.platform.service.TTherapistService;
import com.gxa.meiye.core.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Api
@RestController
@RequestMapping("/platform/homepage")
public class HomepageController {

    @Resource
    private TOrderService tOrderService;
    @Resource
    private TShopService tShopService;
    @Resource
    private TTherapistService tTherapistService;
    @Resource
    private TConsumerService tConsumerService;

    @ApiOperation("交易情况当日")
    @GetMapping("/homepage/dealSituation")
    public Map dealSituation(){
        DealSituationVo dealSituationVo = new DealSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        List<TOrder> tOrders = tOrderService.list(new QueryWrapper<TOrder>().ge("pay_time", c.getTime()).and(tOrderQueryWrapper ->
                tOrderQueryWrapper.eq("status", 2).or().eq("status", 5)));
        if (!tOrders.isEmpty()){
            int orderPay = tOrders.size();
            double moneyPay = tOrders.stream().map(tOrder -> tOrder.getPayMoney()).mapToDouble(Double::doubleValue).sum();
            double clearPay = tOrders.stream().filter(tOrder -> tOrder.getStatus()==3)
                    .map(tOrder -> tOrder.getGetMoney()).mapToDouble(Double::doubleValue).sum();
            dealSituationVo.setOrderPay(orderPay);
            dealSituationVo.setClearPay(clearPay);
            dealSituationVo.setMoneyPay(moneyPay);
        }
        jsonResult.setData(dealSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("交易情况七天")
    @GetMapping("/homepage/dealSituation7")
    public Map dealSituation7(){
        DealSituationVo dealSituationVo = new DealSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 7);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String result = sdf.format(c.getTime());
        System.out.println(result);
        List<TOrder> tOrders = tOrderService.list(new QueryWrapper<TOrder>().ge("pay_time", c.getTime()).and(tOrderQueryWrapper ->
                tOrderQueryWrapper.eq("status", 2).or().eq("status", 5)));
        if (!tOrders.isEmpty()){
            int orderPay = tOrders.size();
            double moneyPay = tOrders.stream().map(tOrder -> tOrder.getPayMoney()).mapToDouble(Double::doubleValue).sum();
            double clearPay = tOrders.stream().filter(tOrder -> tOrder.getStatus()==3)
                    .map(tOrder -> tOrder.getGetMoney()).mapToDouble(Double::doubleValue).sum();
            dealSituationVo.setOrderPay(orderPay);
            dealSituationVo.setClearPay(clearPay);
            dealSituationVo.setMoneyPay(moneyPay);
        }
        jsonResult.setData(dealSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("交易情况三十天")
    @GetMapping("/homepage/dealSituation30")
    public Map dealSituation30(){
        DealSituationVo dealSituationVo = new DealSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 30);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        List<TOrder> tOrders = tOrderService.list(new QueryWrapper<TOrder>().ge("pay_time", c.getTime()).and(tOrderQueryWrapper ->
                tOrderQueryWrapper.eq("status", 2).or().eq("status", 5)));
        if (!tOrders.isEmpty()){
            int orderPay = tOrders.size();
            double moneyPay = tOrders.stream().map(tOrder -> tOrder.getPayMoney()).mapToDouble(Double::doubleValue).sum();
            double clearPay = tOrders.stream().filter(tOrder -> tOrder.getStatus()==3)
                    .map(tOrder -> tOrder.getGetMoney()).mapToDouble(Double::doubleValue).sum();
            dealSituationVo.setOrderPay(orderPay);
            dealSituationVo.setClearPay(clearPay);
            dealSituationVo.setMoneyPay(moneyPay);
        }
        jsonResult.setData(dealSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("注册概况当日")
    @GetMapping("/homepage/registerSituation")
    public Map registerSituation(){
        RegisterSituationVo registerSituationVo = new RegisterSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        int n1 = tConsumerService.count(new QueryWrapper<TConsumer>().ge("registration_time", c.getTime()));
        int n2=tShopService.count(new QueryWrapper<TShop>().ge("registration_time",c.getTime()));
        int n3=tTherapistService.count(new QueryWrapper<TTherapist>().ge("registration_time",c.getTime()));
        registerSituationVo.setConsumer(n1);
        registerSituationVo.setTherapist(n3);
        registerSituationVo.setStore(n2);
        jsonResult.setData(registerSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("注册概况七天")
    @GetMapping("/homepage/registerSituation7")
    public Map registerSituation7(){
        RegisterSituationVo registerSituationVo = new RegisterSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.DATE,c.get(Calendar.DATE) - 7);

        int n1 = tConsumerService.count(new QueryWrapper<TConsumer>().ge("registration_time", c.getTime()));
        int n2=tShopService.count(new QueryWrapper<TShop>().ge("registration_time",c.getTime()));
        int n3=tTherapistService.count(new QueryWrapper<TTherapist>().ge("registration_time",c.getTime()));
        registerSituationVo.setConsumer(n1);
        registerSituationVo.setTherapist(n3);
        registerSituationVo.setStore(n2);
        jsonResult.setData(registerSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("注册概况三十天")
    @GetMapping("/homepage/registerSituation30")
    public Map registerSituation30(){
        RegisterSituationVo registerSituationVo = new RegisterSituationVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.DATE,c.get(Calendar.DATE) - 30);

        int n1 = tConsumerService.count(new QueryWrapper<TConsumer>().ge("registration_time", c.getTime()));
        int n2=tShopService.count(new QueryWrapper<TShop>().ge("registration_time",c.getTime()));
        int n3=tTherapistService.count(new QueryWrapper<TTherapist>().ge("registration_time",c.getTime()));
        registerSituationVo.setConsumer(n1);
        registerSituationVo.setTherapist(n3);
        registerSituationVo.setStore(n2);
        jsonResult.setData(registerSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("用户分布情况")
    @GetMapping("/homepage/userDistribution")
    public Map userDistribution(){
        UserDistributionVo registerSituationVo = new UserDistributionVo();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");

        jsonResult.setData(registerSituationVo);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("用户注册趋势")
    @GetMapping("/homepage/userRegister")
    public Map userRegister(){
        Map<String,UserRegisterVo> map = new HashMap();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 7);
        List<TConsumer> tConsumers = tConsumerService.list(new QueryWrapper<TConsumer>().ge("registration_time", c.getTime()));
        List<TTherapist> tTherapists = tTherapistService.list(new QueryWrapper<TTherapist>().ge("registration_time", c.getTime()));
        List<TShop> tShops = tShopService.list(new QueryWrapper<TShop>().ge("registration_time", c.getTime()));
        for (int i=0;i<=7;i++){
            UserRegisterVo userRegisterVo = new UserRegisterVo();
                Calendar cc = Calendar.getInstance();
                cc.set(Calendar.HOUR_OF_DAY, 0);
                cc.set(Calendar.MINUTE, 0);
                cc.set(Calendar.SECOND, 0);
                cc.set(Calendar.MILLISECOND, 0);
                cc.set(Calendar.DATE, cc.get(Calendar.DATE)-i+1);
                Calendar ccc = Calendar.getInstance();
                ccc.set(Calendar.HOUR_OF_DAY, 0);
                ccc.set(Calendar.MINUTE, 0);
                ccc.set(Calendar.SECOND, 0);
                ccc.set(Calendar.MILLISECOND, 0);
                ccc.set(Calendar.DATE, ccc.get(Calendar.DATE)-i);
            if (!tConsumers.isEmpty()){
                int count1 =(int) tConsumers.stream().filter(tConsumer -> tConsumer.getRegistrationTime().getTime() >= ccc.getTimeInMillis() && tConsumer.getRegistrationTime().getTime() < ccc.getTimeInMillis()).count();
                userRegisterVo.setConsumer(count1);
            }
            if (!tTherapists.isEmpty()){
                int count2 =(int) tTherapists.stream().filter(tTherapist -> tTherapist.getRegistrationTime().getTime() >= ccc.getTimeInMillis() && tTherapist.getRegistrationTime().getTime() < ccc.getTimeInMillis()).count();
                userRegisterVo.setMassagist(count2);
            }
            if (!tShops.isEmpty()){
                int count3 =(int) tShops.stream().filter(tShop -> tShop.getRegistrationTime().getTime() >= ccc.getTimeInMillis() && tShop.getRegistrationTime().getTime() < ccc.getTimeInMillis()).count();
                userRegisterVo.setEnterprise(count3);
            }
            map.put((ccc.get(Calendar.MONTH)+1)+"-"+ccc.get(Calendar.DAY_OF_MONTH),userRegisterVo);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }

    @ApiOperation("交易金额趋势")
    @GetMapping("/homepage/dealMoney")
    public Map dealMoney(){
        Map map = new HashMap();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 7);
        List<TOrder> tOrders = tOrderService.list(new QueryWrapper<TOrder>().ge("pay_time", c.getTime()));
        for (int i=0;i<=7;i++){
            Calendar cc = Calendar.getInstance();
            cc.set(Calendar.HOUR_OF_DAY, 0);
            cc.set(Calendar.MINUTE, 0);
            cc.set(Calendar.SECOND, 0);
            cc.set(Calendar.MILLISECOND, 0);
            cc.set(Calendar.DATE, cc.get(Calendar.DATE)-i+1);
            Calendar ccc = Calendar.getInstance();
            ccc.set(Calendar.HOUR_OF_DAY, 0);
            ccc.set(Calendar.MINUTE, 0);
            ccc.set(Calendar.SECOND, 0);
            ccc.set(Calendar.MILLISECOND, 0);
            ccc.set(Calendar.DATE, ccc.get(Calendar.DATE)-i);
            if (!tOrders.isEmpty()){
                double sum = tOrders.stream().filter(tOrder -> tOrder.getPayTime().getTime() >= ccc.getTimeInMillis() && tOrder.getPayTime().getTime() < ccc.getTimeInMillis()).map(tOrder -> tOrder.getPayMoney()).mapToDouble(Double::doubleValue).sum();
                map.put((ccc.get(Calendar.MONTH)+1)+"-"+ccc.get(Calendar.DAY_OF_MONTH),sum);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }
    @ApiOperation("订单趋势")
    @GetMapping("/homepage/dealOrder")
    public Map dealOrder(){
        Map map = new HashMap();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 7);
        List<TOrder> tOrders = tOrderService.list(new QueryWrapper<TOrder>().ge("pay_time", c.getTime()));
        for (int i=0;i<=7;i++){
            Calendar cc = Calendar.getInstance();
            cc.set(Calendar.HOUR_OF_DAY, 0);
            cc.set(Calendar.MINUTE, 0);
            cc.set(Calendar.SECOND, 0);
            cc.set(Calendar.MILLISECOND, 0);
            cc.set(Calendar.DATE, cc.get(Calendar.DATE)-i+1);
            Calendar ccc = Calendar.getInstance();
            ccc.set(Calendar.HOUR_OF_DAY, 0);
            ccc.set(Calendar.MINUTE, 0);
            ccc.set(Calendar.SECOND, 0);
            ccc.set(Calendar.MILLISECOND, 0);
            ccc.set(Calendar.DATE, ccc.get(Calendar.DATE)-i);
            if (!tOrders.isEmpty()){
                int sum =(int) tOrders.stream().filter(tOrder -> tOrder.getPayTime().getTime() >= ccc.getTimeInMillis() && tOrder.getPayTime().getTime() < ccc.getTimeInMillis()).map(tOrder -> tOrder.getPayMoney()).count();
                map.put((ccc.get(Calendar.MONTH)+1)+"-"+ccc.get(Calendar.DAY_OF_MONTH),sum);
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }

}
